//
//  BlueToothListView.m
//  汽车大灯色温蓝牙
//
//  Created by dfwx on 2020/9/10.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "BlueToothListView.h"
#import "BlueViewListTableViewCell.h"

@interface BlueToothListView () <UITableViewDelegate, UITableViewDataSource, BlueViewListTableViewCellDelegate>
@property (nonatomic, strong) UIView *headView1;
@property (nonatomic, strong) UIView *headView2;
@end

@implementation BlueToothListView

#pragma mark - awk

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BlueViewListTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([BlueViewListTableViewCell class])];
    self.contentView.layer.cornerRadius = 15;
    self.contentView.layer.masksToBounds = YES;
    [self.scanButton setTitle:NSLocalizedString(@"StartScaning", @"扫描设备") forState:UIControlStateNormal];
    [self.scanButton setTitle:WWNSLocalizedString(@"StopScaning", @"停止扫描") forState:UIControlStateSelected];
    [self.closeButton setTitle:WWNSLocalizedString(@"CloseButtonTitle", @"关闭") forState:UIControlStateNormal];
    self.titleLabel.text = WWNSLocalizedString(@"EquipmentSelection", @"设备选择");

}

#pragma mark - action

- (IBAction)scanAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(BlueToothListViewStartScan)]) {
            [self.delegate BlueToothListViewStartScan];
        }
    }else {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(BlueToothListViewEndScan)]) {
            [self.delegate BlueToothListViewEndScan];
        }
    }
    
    
}

- (IBAction)closeAction:(UIButton *)sender {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(BlueToothListViewClose)]) {
        [self.delegate BlueToothListViewClose];
    }
}

#pragma mark - tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.connectArray.count;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BlueViewListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BlueViewListTableViewCell" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        
        CBPeripheral *peripheral = self.connectArray[indexPath.row];
        cell.namelabel.text = peripheral.name;
        cell.tag = indexPath.row;
        cell.delegate = self;
        cell.disconnectButton.hidden = NO;
        
    }else {
        CBPeripheral *peripheral = self.dataArray[indexPath.row];
        cell.namelabel.text = peripheral.name;
        cell.disconnectButton.hidden = YES;
        
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.headView1;
    }
    return self.headView2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        CBPeripheral *peripheral = self.dataArray[indexPath.row];
        if (self.delegate && [self.delegate respondsToSelector:@selector(BlueToothListViewClickPeripheral:)]) {
            [self.delegate BlueToothListViewClickPeripheral:peripheral];
        }

    }
}

- (void)BlueViewListTableViewCellDisconnectAction:(NSInteger)row {
    
    CBPeripheral *peripheral = self.connectArray[row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(BlueToothListViewClickDisconnectPeripheral:)]) {
        [self.delegate BlueToothListViewClickDisconnectPeripheral:peripheral];
    }

    
}

- (UIView *)headView1 {
    
    if (!_headView1) {
        _headView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
        _headView1.backgroundColor = [UIColor lightGrayColor];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.tableView.frame.size.width-30, 44)];
        titleLabel.text = WWNSLocalizedString(@"ConnectedDevice", @"已连接设备");
        titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightBold];
        titleLabel.textColor = [UIColor blackColor];
        [_headView1 addSubview:titleLabel];
    }
    return _headView1;
}

- (UIView *)headView2 {
    
    if (!_headView2) {
        _headView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
        _headView2.backgroundColor = [UIColor lightGrayColor];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.tableView.frame.size.width-30, 44)];
        titleLabel.text = WWNSLocalizedString(@"OtherEquipment",@"其他设备");
        titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightBold];
        titleLabel.textColor = [UIColor blackColor];
        [_headView2 addSubview:titleLabel];
    }
    return _headView2;
    
}

- (void)setDataArray:(NSArray *)dataArray {
    
    _dataArray = dataArray;
    [self.tableView reloadData];
}

- (void)setConnectArray:(NSArray *)connectArray {
    
    _connectArray = connectArray;
    [self.tableView reloadData];
}

@end
